{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "58e4f06d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['S__:9821=叁玖贰捌柒玖贰捌柒玖贰捌柒玖贰捌肆E',\n",
       " 'S__:2424=oyoyoyoyoyoyoyoyEP',\n",
       " 'S__:6623=贰陆肆玖肆陆肆玖肆陆肆玖肆陆肆玖贰E',\n",
       " 'S__:2037=捌壹肆捌捌壹肆捌捌壹肆捌捌壹肆捌EP',\n",
       " 'S__:9685=叁捌柒肆叁捌柒肆叁捌柒肆叁捌柒肆零E',\n",
       " 'S__:5989=二三九五八三九五八三九五八三九五六E',\n",
       " 'S__:1561=六二四四六二四四六二四四六二四四EP',\n",
       " 'S__:1197=4788478847884788EP',\n",
       " 'S__:6300=贰伍贰零贰伍贰零贰伍贰零贰伍贰零零E',\n",
       " 'S__:8081=ewewuwewuwewuwewrE']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%run common.ipynb\n",
    "\n",
    "[tokenizer.decode(i) for i in tokenizer.get_batch_data(prefix=False)[1]][:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "62aea6a1",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "model_cls = ModelCLS()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "dbe0768c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 0.25\n",
      "S__:3899=壹伍伍玖柒伍伍玖柒伍伍玖柒伍伍玖陆E 3\n",
      "S__:4966=一九八六五九八六五九八六五九八六四E 3\n",
      "100 1.0\n",
      "S__:8025=ewqpewqpewqpewqppE 1\n",
      "S__:4263=17053705370537052E 0\n",
      "200 1.0\n",
      "S__:1158=ryewryewryewryewEP 1\n",
      "S__:1526=6104610461046104EP 0\n",
      "300 1.0\n",
      "S__:4577=一八三〇九八三〇九八三〇九八三〇八E 2\n",
      "S__:3455=一三八二一三八二一三八二一三八二〇E 2\n",
      "400 1.0\n",
      "S__:3273=壹叁零玖叁叁零玖叁叁零玖叁叁零玖贰E 3\n",
      "S__:5049=20198019801980196E 0\n"
     ]
    }
   ],
   "source": [
    "optimizer = torch.optim.AdamW(params=model_cls.parameters(), lr=1e-4)\n",
    "criterion = torch.nn.CrossEntropyLoss()\n",
    "\n",
    "for epoch in range(500):\n",
    "    label, input_ids, attention_mask = tokenizer.get_batch_data(prefix=False)\n",
    "    label = torch.LongTensor(label).to(device)\n",
    "    input_ids = torch.LongTensor(input_ids).to(device)\n",
    "    attention_mask = torch.LongTensor(attention_mask).to(device)\n",
    "\n",
    "    logits = model_cls(input_ids=input_ids, attention_mask=attention_mask)\n",
    "\n",
    "    loss = criterion(logits, label)\n",
    "    loss.backward()\n",
    "    optimizer.step()\n",
    "    optimizer.zero_grad()\n",
    "\n",
    "    if epoch % 100 == 0:\n",
    "        logits = logits.argmax(1)\n",
    "        acc = (logits == label).sum().item() / len(label)\n",
    "        print(epoch, acc)\n",
    "\n",
    "        for i in range(2):\n",
    "            print(tokenizer.decode(input_ids[i].tolist()), logits[i].item())\n",
    "\n",
    "model_cls.to('cpu')\n",
    "torch.save(model_cls, 'cls.model')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:cuda117]",
   "language": "python",
   "name": "conda-env-cuda117-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
